<?php

use Illuminate\Support\Facades\Route;

if (!function_exists('admin_url')) {
    /**
     * Generate a url for the application.
     *
     * @param string|null $path
     * @param mixed $parameters
     * @param bool|null $secure
     * @return \Illuminate\Contracts\Routing\UrlGenerator|string
     */
    function admin_url($path = null, $parameters = [], $secure = null)
    {
        if (!empty($path)) {
            if (\Illuminate\Support\Str::startsWith($path, '/')) {
                $path = '/admin' . $path;
            } else if (\Illuminate\Support\Str::startsWith($path, 'http://') || \Illuminate\Support\Str::startsWith($path, 'https://')) {
                //不处理
            } else {
                $path = 'admin/' . $path;
            }

        }
        return url($path, $parameters, $secure);
    }
}

function widget_admin_menu($menu = [], $level = 1)
{
    if (!empty($menu)) {
        if ($level == 1) {
            $html = ' <li class="layui-nav-item">';
        } else {
            $html = ' <dd>';
        }
        if (isset($menu['child'])) {
            if ($level == 1) {
                $html .= ('<a><i class="' . $menu['icon'] . '"></i>&emsp;<cite>' . $menu['name'] . '</cite></a>');
            } else {
                $html .= ('<a><cite>' . $menu['name'] . '</cite></a>');
            }
            //目录
            $html .= '<dl class="layui-nav-child">';
            foreach ($menu['child'] as $children) {
                $html .= widget_admin_menu($children, $level + 1);
            }
            $html .= '</dl>';
        } else {
            if (\Illuminate\Support\Str::startsWith($menu['url'], 'http') || \Illuminate\Support\Str::startsWith($menu['url'], '//')) {
                $html .= ('<a target="_blank" href="' . $menu['url'] . '"><i class="' . $menu['icon'] . '"></i>&emsp;<cite>' . $menu['name'] . '</cite></a>');
            } else {
                $html .= ('<a lay-href="' . admin_url($menu['url']) . '"><i class="' . $menu['icon'] . '"></i>&emsp;<cite>' . $menu['name'] . '</cite></a>');
            }

        }
        if ($level == 1) {
            $html .= ' </li>';
        } else {
            $html .= ' </dd>';
        }

        return $html;
    }
    $menus = \App\Facade\AppContext::menus();
    $html = '';
    foreach ($menus as $menu) {
        $html .= widget_admin_menu($menu, 1);
    }
    return $html;
}

function crudRoute($controller)
{
    Route::get('', $controller . '@index')->middleware('permission');
    Route::get('pager', $controller . '@pager')->middleware('permission');
    Route::get('create', $controller . '@create')->middleware('permission');
    Route::get('edit/{id}', $controller . '@edit')->middleware('permission');
    Route::post('save', $controller . '@save')->middleware('permission');
    Route::post('update', $controller . '@update')->middleware('permission');
    Route::post('batchDelete', $controller . '@batchDelete')->middleware('permission');
    Route::post('delete', $controller . '@delete')->middleware('permission');
}

if (!function_exists('queue_name')) {
    /**
     * 队列名称
     * 解决多个项目队列冲突问题
     *
     * @param $name
     * @return string
     */
    function queue_name($name)
    {
        $name_trans = [];
        $prefix = config('queue.prefix');
        foreach (explode(',', $name) as $k => $v) {
            if (!empty($prefix)) {
                $v = $prefix . '-' . $v;
            }
            $name_trans[] = $v;
        }
        return implode(',', $name_trans);
    }
}
